package com.ehotting.edsta.driverAdapt.controller;

import com.ehotting.eaf.core.domain.R;
import com.ehotting.eaf.security.service.TokenService;
import com.ehotting.edsta.common.log.annotation.Log;
import com.ehotting.edsta.common.log.enums.BusinessType;
import com.ehotting.edsta.driverAdapt.domain.CommonBrowserRequest;
import com.ehotting.mqbd.api.MqActionConstants;
import com.ehotting.mqbd.api.bean.*;
import com.ehotting.mqbd.api.enums.TopicEnum;
import com.ehotting.mqbd.core.service.MQService;
import com.ehotting.mqbd.core.manager.AgentLinkManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/project")
@Api(tags = "ProjectController",description = "驱动程序转发controller")
public class ProjectController {

    @Resource(name="mqService")
    private MQService mqService;

    @Autowired
    private TokenService tokenService;

    private long HTTP_TIMEOUT = 60 * 20;
    @Autowired
    private MqApplicationContext mqApplicationContext;
    @Autowired
    private AgentLinkManager agentLinkManager;

    @Log(title = "驱动程序模块", businessType = BusinessType.OTHER)
    @ApiOperation("驱动程序请求包转发")
    @PostMapping("/rePost")
    public R<CommonResponse> rePost(@RequestBody CommonRequest request) {


        Long userId = tokenService.getLoginUser().getUserid();
        String agentId = agentLinkManager.genAgentId(mqApplicationContext.getModule(),userId,request.getMainProjectId());

        MQHttpData data = MQHttpData.create(
                TopicEnum.CONCURRENCY,
                mqApplicationContext,agentId,
           "");
        data.setAction(MqActionConstants.RE_POST);
        data.setRequest(request);
        data.setAuthorization( tokenService.createJwt(request.getAccessEntranceToken()));
        data.setLogRequest(false);
        data.setLogResponse(false);
        MQHttpData response = (MQHttpData) mqService.request(data, HTTP_TIMEOUT);

        return R.ok(response.getResponse());

    }

    @Log(title = "驱动程序模块文件上传", businessType = BusinessType.OTHER)
    @ApiOperation("驱动程序上传文件的请求包转发")
    @PostMapping("/rePostUpload")
    public R<CommonResponse> rePostUpload(@RequestBody CommonRequest request) {

        Long userId = tokenService.getLoginUser().getUserid();
        String agentId = agentLinkManager.genAgentId(mqApplicationContext.getModule(),userId,request.getMainProjectId());
        MQHttpData data = MQHttpData.create(
                TopicEnum.CONCURRENCY,
                mqApplicationContext,agentId,
                "");
        data.setAction(MqActionConstants.RE_UPLOAD);
        data.setRequest(request);
        data.setAuthorization( tokenService.createJwt(request.getAccessEntranceToken()));
        data.setLogRequest(false);
        data.setLogResponse(false);
        MQHttpData response = (MQHttpData)mqService.request(data, HTTP_TIMEOUT);
        return R.ok(response.getResponse());
    }

    @Log(title = "驱动程序B端模块", businessType = BusinessType.OTHER)
    @ApiOperation("驱动程序请求包转发（web端）")
    @PostMapping("/rePostByBrowser")
    public R<CommonResponse> rePostByBrowser(@RequestBody CommonBrowserRequest request) {

        Long userId = tokenService.getLoginUser().getUserid();
        String agentId = agentLinkManager.genAgentId(mqApplicationContext.getModule(),userId,"");

        MQHttpData data = MQHttpData.create(
                TopicEnum.CONCURRENCY,
                mqApplicationContext,agentId,
                "");
        data.setAction(MqActionConstants.RE_POST_BY_BROWSER);
        data.setRequest(transToCommonRequest(request));
        data.setAuthorization( tokenService.createJwt(request.getAccessEntranceToken()));
        data.setLogRequest(true);
        data.setLogResponse(true);
        MQHttpData response = (MQHttpData) mqService.request(data, HTTP_TIMEOUT);
        return R.ok(response.getResponse());

    }


    @Log(title = "驱动程序B端模块文件上传", businessType = BusinessType.OTHER)
    @ApiOperation("驱动程序上传文件的请求包转发（web端）")
    @PostMapping("/rePostUploadByBrowser")
    public R<CommonResponse> rePostUploadByBrowser(@RequestBody CommonBrowserRequest request) {

        Long userId = tokenService.getLoginUser().getUserid();
        String agentId = agentLinkManager.genAgentId(mqApplicationContext.getModule(),userId,"");

        MQHttpData data = MQHttpData.create(
                TopicEnum.CONCURRENCY,
                mqApplicationContext,agentId,
                "");
        data.setAction(MqActionConstants.RE_UPLOAD_BY_BROWSER);
        data.setRequest(transToCommonRequest(request));
        data.setAuthorization( tokenService.createJwt(request.getAccessEntranceToken()));
        data.setLogRequest(false);
        data.setLogResponse(false);
        MQHttpData response = (MQHttpData) mqService.request(data, HTTP_TIMEOUT);
        return R.ok(response.getResponse());

    }

    private CommonRequest transToCommonRequest(@RequestBody CommonBrowserRequest request) {
        CommonRequest target = new CommonRequest();
        target.setRequestUrl(request.getRequestUrl());
        target.setAccessEntranceToken(request.getAccessEntranceToken());
        target.setHeaders(request.getHeaders());
        target.setWebInfo(request.getWebInfo());
        target.setRequestBody(request.getRequestBody());
        target.setRequestMethod(request.getRequestMethod());
        return target;
    }

}
